package yanyan.com.tochar.adpter;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;
import java.util.Random;

import yanyan.com.tochar.R;
import yanyan.com.tochar.beans.HomeListBean;
import yanyan.com.tochar.gif.ImageUtil;

public class DiaLogListAdapter extends BaseAdapter {
    private List<PackageInfo> packageInfos;
    private LayoutInflater inflater;
    private Context context;

    public DiaLogListAdapter(Context context, List<PackageInfo> packageInfos){
        this.context=context;
        this.packageInfos=packageInfos;
        inflater=LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return packageInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return packageInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView=inflater.inflate(R.layout.item_common_list,null);
            viewHolder.imageView=convertView.findViewById(R.id.common_list_image);
            viewHolder.title=(TextView) convertView.findViewById(R.id.common_list_title);
            viewHolder.text=(TextView) convertView.findViewById(R.id.common_list_text);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }

        PackageInfo packageInfo = packageInfos.get(position);
        ApplicationInfo applicationInfo = packageInfo.applicationInfo;
        String appName= applicationInfo.loadLabel(context.getPackageManager()).toString();
        Drawable appIcon = applicationInfo.loadIcon(context.getPackageManager());
        String packageName = applicationInfo.packageName;
        Bitmap bitmap = drawableToBitmap(appIcon);
        viewHolder.imageView.setImageBitmap(bitmap);
        viewHolder.title.setText(appName);
        viewHolder.text.setText(packageName);
        return  convertView;

    }

    /**
     * Drawable转换成一个Bitmap
     *
     * @param drawable drawable对象
     * @return
     */
    public  Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

    class ViewHolder{
        public TextView title,text;
        public ImageView imageView;
    }
}
